<?php
//通知、支付中心和差账管理
//于婧雯
namespace App\Http\Controllers\Admin\Finance;

use Illuminate\Http\Request;
use App\Http\Controllers\AdapterWebController;
use App\Models\Finance\Cost;
use App\Models\Finance\Repair;
use App\Models\Finance\News;
use App\Models\Finance\IncomeExcel;
use App\Models\Finance\Income;
use App\Modules\Finance\ApiNoticeAndPay;
use Excel;

class NoticeAndPayController extends AdapterWebController
{

    //差账管理 列表 搜索
    public function priceDifference(Request $request){
        if($request->isMethod('get')){
            $difference = $this->appLayerExec(ApiNoticeAndPay::class, 'priceDifference');
            return view('admin.finance.price_difference',['list'=>$difference['res']]);
        }
        $postData = array_filter($request->all());
        $difference = $this->appLayerExec(ApiNoticeAndPay::class, 'priceDifference', $postData);

        return view('admin.finance.price_difference',['list'=>$difference['res']]);
    }
    //差账管理 添加
    public function addPriceDifference(Request $request){
        if($request->isMethod('get')){            
            return view('admin.finance.price_difference_pop');
        }
        $postData = $request->except('_token');
        if (!isset($postData['status'])||empty($postData['status'])) {
            $postData['status'] = 0;
        }
        $postData['time'] = date('Y-m',strtotime($postData['time']));
        $adds = $this->appLayerExec(ApiNoticeAndPay::class, 'addPriceDifference', $postData);
        flash('添加成功')->success();
        return view('admin.finance.price_difference_pop');
    }
    //通知 列表
    public function financeInform(Request $request){
        if($request->isMethod('get')){
            $inform = $this->appLayerExec(ApiNoticeAndPay::class, 'financeInform');
            return view('admin.finance.inform',['list'=>$inform['res']]);
        }
        $postData = array_filter($request->all());
        $articles = $this->appLayerExec(ApiNoticeAndPay::class, 'financeInform', $postData);

        return view('admin.finance.inform',['list'=>$inform['res']]);
    }

    //发送音乐人账单
    public function changeInformMusic($id=0){
        $status = \App\Models\Finance\Monthstatis::where('id','=',$id)->update(['music_status'=>1]);
        $data['type'] = 0;
        $time = \App\Models\Finance\Monthstatis::where('id','=',$id)->get()->toArray();
        $data['content'] = '您的'.$time[0]['time'].'账单到啦，请查看！';
        $news = \App\Models\Finance\News::insert($data);
    }
    //发送代理账单
    public function changeInformAgent($id=0){
        $status = \App\Models\Finance\Monthstatis::where('id','=',$id)->update(['agent_status'=>1]);
        $data['type'] = 1;
        $time = \App\Models\Finance\Monthstatis::where('id','=',$id)->get()->toArray();
        $data['content'] = '您的'.$time[0]['time'].'账单到啦，请查看！';
        $news = \App\Models\Finance\News::insert($data);
    }
    //发送指定人账单
    public function changeInformAppoint($id=0){
        //修改月统计表状态
        $status = \App\Models\Finance\Monthstatis::where('id','=',$id)->update(['appoint_status'=>1]);
        //找到月统计表的年月
        $time = \App\Models\Finance\Monthstatis::where('id','=',$id)->get()->toArray();
        $times = explode('-',$time[0]['time']);
        $_where['year'] = $times[0];
        $_where['month'] = ltrim($times[1],"0");
        $_where['income_excel.status'] = 2;
        //找到指定人id
        $user = \App\Models\Finance\Income::join('income_excel', 'income.id', '=', 'income_excel.income_id')
        ->select('income_excel.user_id')
        ->where($_where)
        ->get()
        ->toArray();
        //return($user);
        $id = [];
        foreach ($user as $k=>$v) {
            $id[$k] = $v['user_id'];
        }
        $data['type'] = 2;
        $data['content'] = '您的'.$time[0]['time'].'账单到啦，请查看！';
        for ($i=0;$i < count($id);$i++) {
            $data['info_id'] = $id[$i];
            \App\Models\Finance\News::insert($data);
        }

    }

    //支付中心 列表
    public function financePayment(Request $request){
        if($request->isMethod('get')){
        //8个统计数据
        $financeCount = $this->appLayerExec(ApiNoticeAndPay::class, 'financeCount');
        //dd($financeCount);
        //音乐人 已完成提现
        $alwayslist1 = $this->appLayerExec(ApiNoticeAndPay::class, 'alwayslist1');
        //代理 已完成提现
        $alwayslist2 = $this->appLayerExec(ApiNoticeAndPay::class, 'alwayslist2');
        //音乐人 已申请提现
        $applylist1 = $this->appLayerExec(ApiNoticeAndPay::class, 'applylist1');
        //代理 已申请提现
        $applylist2 = $this->appLayerExec(ApiNoticeAndPay::class, 'applylist2');
        //音乐人 当前未提现
        $nolist1 = $this->appLayerExec(ApiNoticeAndPay::class, 'nolist1');
        //代理 当前未提现
        $nolist2 = $this->appLayerExec(ApiNoticeAndPay::class, 'nolist2');
        return view('admin.finance.finance_payment',['financeCount'=>$financeCount['res'], 'alwayslist1'=>$alwayslist1['res'], 'alwayslist2'=>$alwayslist2['res'], 'applylist1'=>$applylist1['res'], 'applylist2'=>$applylist2['res'], 'nolist1'=>$nolist1['res'], 'nolist2'=>$nolist2['res']]);
        }

        $rqData = $request->only('action');
        $postData = array_filter($request->all());
        if (!isset($rqData['action']) || empty($rqData['action'])) {
            $postData['action'] = 'music_action1';
        }
        //8个统计数据
        $financeCount = $this->appLayerExec(ApiNoticeAndPay::class, 'financeCount',$postData);
        //音乐人 已完成提现
        $alwayslist1 = $this->appLayerExec(ApiNoticeAndPay::class, 'alwayslist1',$postData);
        //代理 已完成提现
        $alwayslist2 = $this->appLayerExec(ApiNoticeAndPay::class, 'alwayslist2',$postData);
        //音乐人 已申请提现
        $applylist1 = $this->appLayerExec(ApiNoticeAndPay::class, 'applylist1',$postData);
        //代理 已申请提现
        $applylist2 = $this->appLayerExec(ApiNoticeAndPay::class, 'applylist2',$postData);
        //音乐人 当前未提现
        $nolist1 = $this->appLayerExec(ApiNoticeAndPay::class, 'nolist1',$postData);
        //代理 当前未提现
        $nolist2 = $this->appLayerExec(ApiNoticeAndPay::class, 'nolist2',$postData);
        return view('admin.finance.finance_payment',['financeCount'=>$financeCount['res'], 'alwayslist1'=>$alwayslist1['res'], 'alwayslist2'=>$alwayslist2['res'], 'applylist1'=>$applylist1['res'], 'applylist2'=>$applylist2['res'], 'nolist1'=>$nolist1['res'], 'nolist2'=>$nolist2['res']]);
    }

    //导出excel报表
    public function exportToExcel()
    {
        $title = [
            '账单ID',
            '收款人',
            '收款账户',
            '已提现金额',
            '提现分类',
            '提现时间',
            '处理时间'
        ];
        $arr = $this->appLayerExec(ApiNoticeAndPay::class, 'payListNoPage')['res'];
        array_unshift($arr, $title);
        Excel::create('支付中心报表', function($excel) use($arr) {
            $excel->sheet('musicList', function ($sheet) use($arr) {
//                $sheet->setWidth('A', 40);
                $sheet->fromArray($arr, null, 'A1', false, false);
            });
        })->export('xlsx');
    }
    
    //音乐人提现
    public function batchMusic($id=0){
        $time = date('Y-m-d H:i:s',time());
        $status = \App\Models\Finance\Pay::where('type','=',0)->where('status','=',0)->update(['status'=>1,'deal_time'=>$time]);
/*        $ids = \App\Models\Finance\Pay::where('deal_time','=',$time)->where('type','=',0)->get()->toArray();
        $id = [];
        foreach ($ids as $k=>$v) {
            $id[$k] = $v['info_id'];
        }*/
        $data['type'] = 0;
        $data['content'] = '您的申请已结算，平台将在7个工作日内结算完成！';
/*        for ($i=0;$i < count($id);$i++) {
            $data['info_id'] = $id[$i];*/
            \App\Models\Finance\News::insert($data);
/*        } */       

    }
    //代理提现
    public function batchAgent($id=0){
        $time = date('Y-m-d H:i:s',time());
        $status = \App\Models\Finance\Pay::where('type','=',1)->where('status','=',0)->update(['status'=>1,'deal_time'=>$time]);
/*        $ids = \App\Models\Finance\Pay::where('deal_time','=',$time)->where('type','=',1)->get()->toArray();
        $id = [];
        foreach ($ids as $k=>$v) {
            $id[$k] = $v['info_id'];
        }*/
        $data['type'] = 1;
        $data['content'] = '您的申请已结算，平台将在7个工作日内结算完成！';
/*        for ($i=0;$i < count($id);$i++) {
            $data['info_id'] = $id[$i];*/
            \App\Models\Finance\News::insert($data);
/*        }*/
    }


}
